//
//  RecommendViewController.swift
//  WhyTest
//
//  Created by 宋广越 on 2022/2/18.
//

import UIKit

private let kItemMargin : CGFloat = 10
private let kItemW = (kScreenW - 3 * kItemMargin) / 2
private let kNormalItemH = kItemW * 3 / 4
private let kNomalCellID = "kNomalCellID"
private let kPrettyItemH = kItemW * 4 / 3
private let kHeaderViewID = "kHeaderViewID"
private let kPrettyCellID = "kPrettyCellID"
private let kHeaderViewH : CGFloat = 50
class RecommendViewController: UIViewController {

    //懒加载属性
    private lazy  var collectionView : UICollectionView = {[unowned self] in
        //先创建 我们的布局
        let layout  = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kItemW, height: kNormalItemH)
        layout.minimumLineSpacing = 0
        //最小间距
        layout.minimumInteritemSpacing = kItemMargin
        //设置cell的头
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        //设置内边距
        layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
        //创建uicollectionView
        print(self.view.bounds)
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
         //设置代理判断cell 高度
        collectionView.delegate = self
         
        //设置collectionView 随着高度变化而变化
        collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        //获取cell要先注册cell,注册cell
//        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "kNomalCellID")
        //注册一个自定义cell
    collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNomalCellID)
        //注册一个自定义cell2
    collectionView.register(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCellID)
        //如果想在cell头部分放东西,需要注册一个头 注册位置放在头还是尾部
//        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind : UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        //注册一个xib的头
        collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind : UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        return collectionView
    }()
     
    //系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.purple
        //设置UI界面
        setupUI()
       
        
    }
    

}
extension RecommendViewController {
    private func setupUI (){
        //将uicollectionView添加到控制器的View中
        view.addSubview(collectionView)
    }
    
}

extension RecommendViewController : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    ///实现分组,先看总共有多少组,一共12组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 12
    }
    //每组里面有多少数据
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //
        if section == 0{//如果是第一组
            return 8
        }
        return 4
    }
    //
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //获取cell
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNomalCellID, for: indexPath)
        //当有多个cell 时候
        var cell : UICollectionViewCell
        //取出cell
        if indexPath.section == 1{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPrettyCellID, for: indexPath)
        }else{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNomalCellID, for: indexPath)
        }
        
        //cell.backgroundColor = UIColor.red
        return cell
    }
    //定义section 的headerview
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        //取出section的headview
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath)
       
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        if indexPath.section == 1 {
            return CGSize(width: kItemW, height: kPrettyItemH)
        }
        return CGSize(width: kItemW, height: kNormalItemH)
    }
   
}
